package cn.falconnect.adserver.adapter.persistence.dao;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;

import cn.falconnect.adserver.domain.ad.AdTemplate;

@Component
public interface AdTemplateDao {

	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "sample_url", property = "sampleUrl"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime")
	})
	@Select("select * from ad_template")
	List<AdTemplate> findAll();
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "sample_url", property = "sampleUrl"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime")
	})
	@Select("select * from ad_template where update_time >= #{value}")
	List<AdTemplate> findAfterTime(String warmUpTime);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "sample_url", property = "sampleUrl"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime")
	})
	@Select("select * from ad_template where id = #{value}")
	AdTemplate findById(int id);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "sample_url", property = "sampleUrl"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime")
	})
	@Select("select * from ad_template where id = #{id} and status = #{status}")
	AdTemplate selectById(@Param("id")long id, @Param("status")int status);
	
	@Results(value = { @Result(column = "id", property = "id", id = true),
			@Result(column = "sample_url", property = "sampleUrl"),
			@Result(column = "create_time", property = "createTime"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "id",property ="adTemplateSpecs",many = @Many(select = "cn.falconnect.adserver.adapter.persistence.dao.AdTemplateSpecDao.findByTemplateId" ,fetchType = FetchType.DEFAULT))
	})
	@Select("select * from ad_template where id = #{id} and status = 1")
	AdTemplate selectByIdWithSpecs(@Param("id")long id);

}
